package com.aiju.cash.ui.multiple_shops.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiju.cash.R;
import com.aiju.cash.ui.multiple_shops.bean.BaseMenu;

import java.util.List;

/**
 * Created by Administrator on 2017/6/12.
 */

public class MenuLinearLayout extends LinearLayout {
    private Context mContext;

    public MenuLinearLayout(Context context) {
        super(context);
        initView(context);
    }

    public MenuLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public MenuLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    void initView(Context c) {
        mContext = c;
        setOrientation(VERTICAL);
        setGravity(Gravity.BOTTOM);

        setDividerDrawable(c.getResources().getDrawable(R.drawable.shape_popuwindow_item_divider));
        setShowDividers(SHOW_DIVIDER_MIDDLE);
        setDividerPadding(dip2px(mContext,15));
        setBackgroundResource(R.mipmap.icon_shop_manager_popu_bg);
    }

    /**
     * 设置数据形成linearlayout
     */
    public void setData(List<BaseMenu> list) {
        for (int i = 0; i < list.size(); i++) {
            final BaseMenu baseMenu = list.get(i);
            LinearLayout view = (LinearLayout)LayoutInflater.from(mContext).inflate(R.layout.item_popuwindow, null);
            LayoutParams layoutParams = new LayoutParams(dip2px(mContext, 150), dip2px(mContext, 45));
            view.setLayoutParams(layoutParams);
            ImageView imageView = (ImageView) view.findViewById(R.id.image);
            TextView textView = (TextView) view.findViewById(R.id.name);
            imageView.setImageResource(baseMenu.getResId());
            textView.setText(baseMenu.getTitle());
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    menuItemCickLister.onClick(baseMenu.getId());
                }
            });
            addView(view);
        }
    }

    private MenuItemCickLister menuItemCickLister;

    public interface MenuItemCickLister {
        void onClick(int position);
    }

    public void setListener(MenuItemCickLister listener) {
        menuItemCickLister = listener;
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


//    public static void setMargins (View v, int l, int t, int r, int b) {
//        if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
//            ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
//            p.setMargins(l, t, r, b);
//            v.requestLayout();
//        }
//    }
}
